package com.funo.main.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.funo.common.pojo.PageResult;
import com.funo.common.pojo.QueryCondition;
import com.funo.common.pojo.TreeNode;
import com.funo.common.utils.Constant;
import com.funo.main.dao.RoleDao;
import com.funo.main.dao.UserRoleDao;
import com.funo.main.entities.Role;
import com.funo.main.entities.User;
import com.funo.main.entities.UserRole;
import com.funo.main.service.RoleService;

/**
 * 角色管理业务接口实现类
 * 
 * @author zhengjs
 *
 */
@Service
@Transactional
public class RoleServiceImpl implements RoleService{
	
	@Autowired
	private RoleDao roleDao;//角色管理DAO
	
	@Autowired
	private UserRoleDao userRoleDao;//用户角色DAO

	@Override
	public Integer addRole(User user,Role role) {
		
		Integer type=Constant.STATUS_ABNORMAL;//初始为0
		
		Role roleTl=roleDao.getRole(role.getName());//通过角色名,获取角色信息.
		
		if(roleTl !=null && roleTl.getId() !=null){//重复
			type = Constant.STATUS_REPEAT;//重复标识,2
		}else{
			role.setId(UUID.randomUUID().toString());//主键ID
			role.setOpUserName(user.getName());//操作人
			//role.setOrgId(orgId);//机构ID
			role.setRoleLevel(Constant.ROLE_LEVEL_FUNCTION);//角色等级
			role.setOpTime(new Date());//操作时间
			
			roleDao.save(role);//保存操作
			
			type = Constant.STATUS_SUSSESS;//成功标识,1
		}
		
		return type;
	}

	@Override
	public void deleteRole(Role role) {
		roleDao.delete(role.getId());
	}

	@Override
	public Integer updateRole(User user,Role role) {
		
		Integer type = Constant.STATUS_ABNORMAL;
		
		//通过角色名和主键id，查询角色信息。
		Role roleTil = roleDao.findRoleByName(role.getName(), role.getId());
		
		if(roleTil !=null && roleTil.getId() !=null){//重复
			type = Constant.STATUS_REPEAT;
		}else{
			//通过id，查询角色信息。
			Role oldRole = roleDao.get(role.getId());
			
			oldRole.setName(role.getName());//角色名
			oldRole.setComments(role.getComments());//备注
			oldRole.setOpUserName(user.getName());//操作人
			oldRole.setOpTime(new Date());//操作时间
			
			roleDao.update(oldRole);//更新操作
			
			type = Constant.STATUS_SUSSESS;
		}

		return type;
	}

	@Override
	public Role getRole(String id) {
		return roleDao.get(id);
	}

	@Override
	public PageResult<Role> getRolePage(QueryCondition condition) {
		
		PageResult<Role> page = new PageResult<Role>();
		
		page.setList(roleDao.pageList(condition));
		page.setTotal(roleDao.count(condition));
		
		return page;
	}

	@Override
	public void checkUserRole(UserRole userRole, boolean checked) {
		
		if(checked){
			userRole.setId(UUID.randomUUID().toString());
			userRoleDao.save(userRole);
		}else{
			userRoleDao.deleteByHql(userRole);
		}
	}
	
	@Override
	public TreeNode moduleTree() {
		
		TreeNode root = new TreeNode();
		
		root.setChildren(modules2TreeNodes(roleDao.getRole()));

		return root;
	}
	
	public List<TreeNode> modules2TreeNodes(List<Role> roles) {
		
		List<TreeNode> treeNodes = new ArrayList<TreeNode>();
		
		TreeNode treeNode;
		
		for (Role role : roles) {
			treeNode = new TreeNode(role.getId(), role.getName(), null, "Y", null, true);
			treeNodes.add(treeNode);
		}
		
		return treeNodes;
	}
	
	public List<TreeNode> modules2TreeNodes(List<Role> roles,List<UserRole> userRoles){
		
		List<TreeNode> treeNodes = new ArrayList<TreeNode>();
		
		for (Role role : roles) {
			TreeNode treeNode = new TreeNode(role.getId(), role.getName(), null, null, false, true);
			treeNodes.add(treeNode);
		}
		
		if(userRoles!=null && userRoles.size()>0){
			
			for (TreeNode treeNode : treeNodes) {
				
				for (Iterator<UserRole> iterator = userRoles.iterator(); iterator.hasNext();) {
					
					UserRole userRole = iterator.next();
					
					if(treeNode.getId().equals(userRole.getRole().getId())){
						treeNode.setChecked(true);
						iterator.remove();
					}
				}
			}
		}
		
		return treeNodes;
	}
	
	@Override
	public TreeNode userRoleTree(String id) {
		
		if("root".equals(id.toLowerCase())){
			return moduleTree();
		}else{
			
			TreeNode root = new TreeNode();
			
			List<UserRole> userRoles = userRoleDao.getByRole(id);
			
			root.setChildren(modules2TreeNodes(roleDao.getRole(),userRoles));
			
			return root;
		}
	}
}
